<template>
	<view>
		<tab-nav @switchStatus="switchStatus" :orderStaIndex="orderStaIndex" :margTop="0" :orderStaArr="orderStaArr"></tab-nav>
		<view class="recordWrap">
			<view class="recordTit">
				<template v-if="loadFlag">
					<view v-if="currRole == 'jz'">最近{{diffday}}天孩子【{{stuName}}】学习过的<text class="subject">{{subName}}</text>知识点如下:</view>
					<view v-else>最近{{diffday}}天<text class="subject">【{{currClsName}}-{{stuName}}】</text>学习过的<text class="subject">{{subName}}</text>知识点如下:</view>
				</template>
				<template v-else>
					<loading></loading> 
				</template>
			</view>
			<view class="listWrap">
				<template v-if="isHasDataFlag == true">
					<scroll-view class="scrollView" scroll-y>
						<record-list :recList="recList" @goRecDet="goRecDet"></record-list>
					</scroll-view>
				</template>
				<template v-else-if="isHasDataFlag == false">
					<no-data></no-data>
				</template>
			</view>
			<record-tj :loadFlag="loadFlag" :recField="recField"></record-tj>
		</view>
		<uni-drawer :visible="showRight" mode="right" :isHadFlDataFlag="true" :zIndex="999" @close="closeDrawer">
			<filters
				:currRole="currRole"
				:clsList="clsList"
				:childList="childList"
				:subDataNum='subDataNum'
				:stuDataNum="stuDataNum"
				:childDataNum="childDataNum"
				:subListQt="subListQt"
				:subArr="subArr"
				:subIndex="subIndex"
				:clsIndex="clsIndex"
				:startDate="startDate"
				:endDate="endDate"
				:stuName="stuName"
				@selectCurrSub="selectCurrSub"
				@sureFilter="sureFilter"
				@bindDateChange="bindDateChange"
				@bindDateChangeEnd="bindDateChangeEnd"
				@selectCls="selectCls"
				@selectChild="selectChild"
				@switchStu="switchStu"
			/>
		</uni-drawer>
	</view>
</template>

<script>
	import TabNav from '@/components/tab-nav/tab-nav.vue'
	import RecordList from '../learnRecord/components/RecordList'
	import RecordTj from '../learnRecord/components/RecordTj'
	import Filters from '../learnRecord/components/Filter'
	import { getStudyLogList,getMyChilds } from '@/api/zhuxue'
	import getRecord from './mixins/getRecord.js'
	export default {
		components:{
			TabNav,
			RecordList,
			RecordTj,
			Filters
		},
		mixins:[getRecord],
		data() {
			return {
				orderStaArr:[
					{id:0,name:'全部'},
					{id:1,name:'未完成'},
					{id:2,name:'已完成'}
				],
				orderStaIndex:0,
				stuAccount:'',
				recList:[],
				dataNum:-1,//-1 未选择学生 0 无记录 1有记录
				isHasDataFlag:null,
				diffday:0,
				recField:{},
				loadFlag:false,
				isFinishSta:0,
				currRole:'',
				noDataTxt:'',
				childDataNum:-1,
				childList:[],
				currPage:'recordDet',
				classId:0
			}
		},
		onLoad(option){
			var title = '';
			this.currRole = option.currRole;
			this.noDataTxt = this.currRole == 'jz' ? '请选择孩子' : '请选择学生';
			if(this.currRole == 'tea'){
				// let recFieldTea = uni.getStorageSync('recFieldTea');
				// this.classId = option.classId;
				// this.stuName = recFieldTea.stuName;
				// this.stuId = recFieldTea.stuId;
				// this.startDate = recFieldTea.startDate;
				// this.endDate = recFieldTea.endDate;
				// this.subId = recFieldTea.subId;
				// title = recFieldTea.stuName + '学习记录';
				this.loadMyClsList();
			}else{
				title = '孩子学习记录';
				this.loadMyChilds();
			}
			setTimeout(()=>{
				uni.setNavigationBarTitle({
					title
				})
			},100)
		},
		onUnload(){
			uni.removeStorageSync('stuList');
		},
		methods: {
			//获取我的孩子 家长用
			loadMyChilds(){
				let field = { account:uni.getStorageSync('token') };
				let contentStr = this.crypto.encrypt(JSON.stringify( field )); 
				getMyChilds({content:contentStr}).then(res=>{
					this.childDataNum = 1;
					this.childList = res.data.datas;
					//默认加载第一个孩子的学习记录
					this.stuName = this.childList[0].stuRealName;
					this.stuAccount = this.childList[0].stuAccount;
					this.clsIndex = 0;
					this.getSubList('parent','','handleClick');
					
				}).catch(err=>{
					this.childDataNum = 0;
					console.log(err)
				});
			},
			selectChild(index,item){
				this.clsIndex = index;
				//this.stuId = item.stuUserId;
				this.stuAccount = item.stuAccount;
				this.stuName = item.stuRealName;
				//this.dataNum = -1;
				this.isFinishSta = 0;
				//获取学科
				this.getSubList('parent','','handleClick');
			},
			//切换学生
			switchStu(){
				uni.navigateTo({
					url:'/pages/subIndex/stuList/stuList?currClsName=' + this.currClsName + '&stuName=' + this.stuName + '&fromPage=recordPage'
				})
			},
			getStudyLogList(){
				//stuId:this.stuId
				var field = { account:this.stuAccount,subId:this.subId,sDate:this.startDate,eDate:this.endDate,isFinish:this.isFinishSta };
				console.log(field);
				let contentStr = this.crypto.encrypt(JSON.stringify( field )); 
				getStudyLogList({content:contentStr}).then(res=>{
					//console.log(res)
					var tmpRes = res.data.datas;
					this.startDate = tmpRes.sDate;
					this.endDate = tmpRes.eDate;
					this.diffday = tmpRes.diffday;
					this.subName = tmpRes.subName;
					this.loadFlag = true;
					this.recField = { diffday:tmpRes.diffday,finishNum:tmpRes.finish,noFinishNum:tmpRes.noFinish,totalNum:tmpRes.allStudyLog,comRate:tmpRes.comRate };
					if(tmpRes.studyList.length > 0){
						this.isHasDataFlag = true;
						this.recList = tmpRes.studyList;
					}else{
						this.recList = [];
						this.isHasDataFlag = false;
					}
				}).catch(err=>{
					this.isHasDataFlag = false;
					console.log(err)
				});
			},
			goRecDet(item){
				uni.navigateTo({
					url:'/pages/subIndex/learnRecDet/learnRecDet?studyLogId=' + item.studyLogId + '&loreName=' + item.loreName + '&currRole=' + this.currRole + '&stuName=' + this.stuName
				})
			},
			onNavigationBarButtonTap(){
				this.showRight = true;
			},
			switchStatus(index,id){
				this.orderStaIndex = index;
				this.isFinishSta = id;
				this.getStudyLogList();
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/static/scss/learnRecord.scss'
</style>